package com.szxy.frontend.search.service.impl;

import com.szxy.frontend.search.service.SolrService;
import com.szxy.mapper.SolrItemMapper;
import com.szxy.pojo.SolrItem;
import com.szxy.utils.Result;
import com.szxy.utils.SolrDocument;
import org.apache.solr.common.SolrInputDocument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.solr.core.SolrTemplate;
import org.springframework.data.solr.core.query.Criteria;
import org.springframework.data.solr.core.query.HighlightOptions;
import org.springframework.data.solr.core.query.SimpleHighlightQuery;
import org.springframework.data.solr.core.query.result.HighlightEntry;
import org.springframework.data.solr.core.query.result.HighlightPage;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Auther:zwer
 * @Date:2019/9/11 17:19
 * @Description:com.szxy.frontend.search.service.impl
 * @Version:1.0
 **/
@Service
public class SolrServiceImpl implements SolrService {

    @Autowired
    private SolrItemMapper solrItemMapper;
    @Autowired
    private SolrTemplate solrTemplate;
    @Value("${spring.data.solr.core}")
    private String collection;


    @Override
    public List<SolrDocument> selectQueryByq(String q, Long page, Integer pageSize) {
        //设置高亮查询条件
        SimpleHighlightQuery query = new SimpleHighlightQuery();
        Criteria criteria = new Criteria();
        criteria.is(q);
        query.addCriteria(criteria);
        
        //设置高亮属性
        HighlightOptions highlightOptions = new HighlightOptions();
        highlightOptions.addField("title");//设置高亮显示的域
        highlightOptions.setSimplePrefix("<em style='color:red'>");//设置高亮显示的前缀
        highlightOptions.setSimplePostfix("</em>");// 设置高亮显示的后缀
        query.setHighlightOptions(highlightOptions);

        //分页
        query.setOffset((page-1)*pageSize);
        query.setRows(pageSize);

        //拿着查询条件去查询
        HighlightPage<SolrDocument> highlightPage =
                this.solrTemplate.queryForHighlightPage(this.collection, query, SolrDocument.class);

        List<HighlightEntry<SolrDocument>> highlightEntryList = highlightPage.getHighlighted();

        for (HighlightEntry<SolrDocument> solrDocumentHighlightEntry : highlightEntryList) {
            //获取实体类对象
            SolrDocument entity = solrDocumentHighlightEntry.getEntity();
            //获取
            List<HighlightEntry.Highlight> highlights = solrDocumentHighlightEntry.getHighlights();
            //如果有高亮，就取高亮
            if(highlights != null && highlights.size() > 0 && highlights.get(0).getSnipplets().size() > 0) {
                entity.setTitle(highlights.get(0).getSnipplets().get(0));
            }
        }
        List<SolrDocument> content = highlightPage.getContent();
        return content;
    }

    @Override
    public Result importAll() {
        try {
            List<SolrItem> solrItemList = this.solrItemMapper.getSolrItemList();
            for (SolrItem solrItem : solrItemList) {
                //创建  SolrInputDocument 对象
                SolrInputDocument inputDocument = new SolrInputDocument();
                inputDocument.addField("id",solrItem.getId());
                inputDocument.addField("title",solrItem.getTitle());
                inputDocument.addField("sell_point",solrItem.getSell_point());
                inputDocument.addField("price",solrItem.getPrice());
                inputDocument.addField("image",solrItem.getImage());
                inputDocument.addField("category_name",solrItem.getCategory_name());
                //保存到索引库
                this.solrTemplate.saveDocument(this.collection,inputDocument);
            }
            this.solrTemplate.commit(this.collection);//提交索引库事务
            return Result.ok();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.error("导入数据失败");
    }
}
